const app = getApp(),url = app.globalData.url;
Page({
  data: {
    newArr:[
      {
        id:4,name:'职场咨询',
        chiden:[
          {id:39,name:'劳动法规'},
          {id:40,name:'个人所得税'},
          {id:41,name:'法定假期'},
        ],
      },
      {
        id:5,name:'本地咨询',
        chiden:[
          {id:42,name:'行业薪资'},
          {id:43,name:'人才政策'},
          {id:44,name:'创业支持'},
          {id:45,name:'社保相关'},
          {id:46,name:'公积金'},
        ],
      }
    ],//一级 没用
    index:5,//一级分类4：职场资讯    5：本地质询
    tit:'资讯',
    fid:'',//二级分类id
    first_type:5,
    new_ArrFlist:[],//二级分类
    fere:{//二级分类的条件
      pageNum:1,
      pageSize:10,
      type :'',
    },
  },
  searchBut(e){
    var that = this
    var index = that.data.index
    var key = e.detail.value
    var subTypeName = that.data.subTypeName
    wx.navigateTo({
      url: './list/list?status='+index+'&key='+key+'&subTypeName='+subTypeName,
    })  
  },
  onLoad (options) {
    console.log(options)
  },
  onShow(){
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({ selected: 2 })
    }
    var that = this
    var index = wx.getStorageSync('si_or_wu')
    if(index != ''){
      that.data.first_type = index
      that.setData({index})
    }
    that.zhichang()
  },
  zhichang(){
    var that = this
    var first_type = wx.getStorageSync('si_or_wu')
    if(first_type == 4){
      var urlapi = 'dict/getWorkplaceInquiryState'
    }else{
      var urlapi = 'dict/getLocalInquiryState'
    }
    
    wx.request({
      url:url+urlapi,
      method: "GET",
      data:{},
      success:function(res){
        console.log("两个资讯",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({
            new_ArrFlist:ret.data,
            subTypeName:ret.data[0].subTypeName//资讯 类型
          })
          that.flist(ret.data[0].id)//在下边 赋值 fid (选中状态)
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  
  // 父类点击
  select(e){
    var that = this
    var index = e.currentTarget.dataset.index
    wx.setStorageSync('si_or_wu',index)
    that.data.first_type = index
    that.data.fere.pageNum = 1
    // console.log(index)
    if(index == 4){
      that.setData({tit:'资讯'})
    }else{
      that.setData({tit:'信息'})
    }
    that.zhichang()
    that.setData({index})
  },
  //子类点击
  fsele(e){
    var that = this
    var fid = e.currentTarget.dataset.fid//分类ID
    var name = e.currentTarget.dataset.name//分类name
    that.data.fere.pageNum = 1
    that.flist(fid)
    that.setData({fid:fid,subTypeName:name})
  },
  flist(fid){
    var that = this
    wx.showLoading({title: '加载中',})
    that.setData({fid})
    that.data.fere.type  =  fid//分类id type {pageNum: 1, pageSize: 10, type: 42}
    var seach = that.data.fere
    console.log(that.data.first_type)
    var sts = that.data.first_type
    if(sts == 5){
      var urlapi = 'common/getInquityLocalByType'
    }else{
      var urlapi = 'common/getInquityWorkplaceByType'
    }
    // return false
    wx.request({
      url:url+ urlapi,
      method: "GET",
      data:seach,
      success:function(res){
        console.log("数据列表",res);
        var ret = res.data
        if(ret.code == 200){
          wx.hideLoading()
          if(sts == 5){
            that.setData({listdata:ret.data.content})//
          }else{
            that.setData({listdata:ret.data})//.content
          }
        }else{
          wx.hideLoading()
          that.setData({listdata:[]})
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })

  },
  // 资讯详情
  newsdetail(e){
    var that = this
    let did = e.currentTarget.dataset.id
    let oneid = that.data.first_type
    let subTypeName = that.data.subTypeName
    wx.navigateTo({
      url: '/pages/newsdetail/newsdetail?id='+did + '&oneid='+oneid +'&subTypeName=' + subTypeName,
    })
  },

  onReachBottom: function () {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    if(that.data.first_type == 5){
      var urlapi = 'common/getInquityLocalByType'
    }else{
      var urlapi = 'common/getInquityWorkplaceByType'
    }
    that.data.fere.pageNum++
    var seach = that.data.fere
    var listdata = that.data.listdata
    wx.request({
      url:url+ urlapi,
      method: "GET",
      data:seach,
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
          wx.hideLoading()
          if(ret.data && ret.data.length){
            listdata=listdata.concat(ret.data.content)
            that.setData({listdata:listdata})
          }else{
            wx.showToast({
              title: '到底了~~',
              icon:'none'
            })
          }

        }else{
          wx.hideLoading()
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this
    that.data.fere.pageNum = 1
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    that.data.fere.pageNum = 1
  },

})